package com.j.lemon.learn.jwt;

import com.nimbusds.jose.jwk.JWK;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;
import java.text.ParseException;
import java.util.Base64;

/**
 * @author lijunjun
 * @title: JwtParse
 * @description: 解析JWK  并验证JWT   RSA加密方式
 * @date 2023/4/617:23
 */
public class JwtParse {
    public static void main(String[] args) throws ParseException {
        String jwks = "{\"kty\":\"RSA\",\"e\":\"AQAB\",\"kid\":\"df8e597e-3aa0-437e-90bf-d6b13df98931\",\"n\":\"z8i9POs68G86G5YpBSxf-AevNHq2tKo6oTLsq_oklNM9wWph2bW6GCAr_xCvOBeUMX9ulst5P7VDE7YsOE_nT-ewdoh-7h4jPkG7Re9rU0YFXTkuBKTKy9IXlz6ye_An8XRvj__AooSKxDLsfhR6XRAkhuCzRqWIRadpuGn43Y5x0gvW5cSd9AvuOJRsip4Q8vkKsEj1njXNNy9BFUN9YPgO1g8hffXuI92l_qxCvGRqRVrrDhNlo3PpzvztYp0cu6wUBc95ITrYsBS73z5ylVfVLxkUWiTccGPCUEyASJ2eVV8KNwpWyBruTyzkqcn7D_UFfYcfKy5Ll3xlwVaEIQ\"}";
        //公钥json里的n
        String publicKeyStr = "oIF4i4ehm1M_cdYS7K_ZCS9c-FVTWdqZzDdjY7mIsEejqX8SPlRRsaPlhJipqULJ_BEnimk3zWcyuLQ-Nzec61w_Nm02OWuL-sxnE_EyEw0jKn5xBQpt_Q0oi0okKGHUEkx8kppFc4qsSYyafT9gwjB8J_ae8mOVJYhoAwXPmrvoZ5valxLbFX3zIF0YzAGkxdfpOG6xlrL_bTEgQHt3AIfej-kQMhDEV_Zj2rA3Zxzn15Pfsvpi_9lLPeJLYBMsMRhVNOkEx9TL5s8eq7ylJ1SFnRLIsmZW1JUXd4xUdp3Mp5G1w1Al16NR5R9POdxQpISQGHlVghgZlcKYeaxu_Q";
        //公钥json里的e
        String exponentStr = "AQAB";
        byte[] modulusBytes =  Base64.getUrlDecoder().decode(publicKeyStr);
        byte[] exponentBytes = Base64.getUrlDecoder().decode(exponentStr);

        BigInteger modulusInt = new BigInteger(1, modulusBytes);
        BigInteger exponentInt = new BigInteger(1, exponentBytes);
        try {
            RSAPublicKeySpec pubKeySpecification = new RSAPublicKeySpec(modulusInt, exponentInt);
            KeyFactory keyFac = KeyFactory.getInstance("RSA");
            RSAPublicKey pubKey = (RSAPublicKey)keyFac.generatePublic(pubKeySpecification);
            JwtParser jwtParser = Jwts.parser();
            jwtParser.setSigningKey(pubKey);
            String token = "eyJraWQiOiI0ZDRhMmJlMy1hZjU5LTQxN2EtYjEwMS0xYmEzZmQxOTgxZmUiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ1c2VyMSIsInpvbmVpbmZvIjoiZSIsImJpcnRoZGF0ZSI6ImUiLCJnZW5kZXIiOiJlIiwiaXNzIjoiaHR0cDpcL1wvbXlkb21haW46OTAwMCIsInByZWZlcnJlZF91c2VybmFtZSI6ImUiLCJsb2NhbGUiOiJlIiwiYXpwIjoiMTIzIiwibmlja25hbWUiOiJlIiwiZXhwIjoxNjgwNzg3NDI5LCJpYXQiOjE2ODA3ODU2MjksImVtYWlsIjoiZSIsIndlYnNpdGUiOiJlIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImFkZHJlc3MiOnsic3RyZWV0X2FkZHJlc3MiOiIiLCJjb3VudHJ5IjoiIiwiZm9ybWF0dGVkIjoiIiwibG9jYWxpdHkiOiIiLCJyZWdpb24iOiIiLCJwb3N0YWxfY29kZSI6IiJ9LCJwcm9maWxlIjoiZSIsInBob25lX251bWJlcl92ZXJpZmllZCI6dHJ1ZSwiZ2l2ZW5fbmFtZSI6ImIiLCJtaWRkbGVfbmFtZSI6ImUiLCJwaWN0dXJlIjoiZSIsImF1ZCI6IjEyMyIsIm5hbWUiOiJhIiwicGhvbmVfbnVtYmVyIjoiZSIsImZhbWlseV9uYW1lIjoiYyJ9.LQgv5Zqj7CtPBmYKOpnDLQSqM-o-ckqJyxU_idu_DA1ydiXUmmwK6Kmkeg63JZzuqOleeeRky7bgAGvvK89ssZbOh0T_BGDlZbiaxbJBJ_7dM-tZGjkMBH5pveVzijwF-iyYMsr-m7_uvcUHu67XquwQTp5Zk8bYfg89pCHHJj86XOSlAb2s28hZYtpP61Sl1UmokmyqNEzKVagiZqmdJGNkM-ZsnXrt5E996chwXuH0gzneEXujBbVp4tBeP_4XfmuHWhEkf1Ar48d5MPut9gbGpZh7Ke53-Goj6BAK1TnMlcf6kmTGSdFglU6XlVZswZPZ5bo2ZDcg4652CJoP_w";
            Claims claims = jwtParser.parseClaimsJws(token).getBody();
            System.out.println(claims);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
